package com.cloudptt.api.models.call;



import java.util.HashMap;
import java.util.Map;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.log.Log;

/**
 * SIP消息扩展部分信息
 * @author TP
 *
 */
public class CallExtStrInfo 
{
	private final static String THIS_FILE = "CallExtStrInfo";
	public static final String CallType_Desc_Group = "AudGrpCall";
	public static final String CallType_Desc_Single_Aud = "AudPriCall";
	public static final String CallType_Desc_Single_Vid = "VidPriCall";
	
	public static final String CallType_Desc_Single_Half = "AudHalfPriCall";
	public static final String CallType_Desc_Single_Vid_Push = "VidPriPush";
	public static final String CallType_Desc_Single_Vid_Pull = "VidPriPull";
	
	public static final String CallType_Desc_Single_Vid_Trans = "VidTrans";
	public static final String CallType_Desc_Single_Vid_TransGrp = "VidTransGrp";
	public static final String CallType_Desc_Single_Vid_PushGrp = "VidPushGrp";
	
	public final static String DefauleValue = "";
	public final static String Key_GId = "GNum";
	public final static String Key_Caller = "caller";
	public final static String Key_Callee = "PNum";
	public final static String Key_UseVideo = "use_video";
	public final static String Key_Direct = "Direct";
	
	private int callType = CallAttr.CallType_Single;
	private String caller = null;
	private String callee = null;
	private boolean useVideo = false;
	
	private String callTypeStr = null;
	private Map<String,String> valueMap = null;

	public CallExtStrInfo(int callType,String caller,String callee,boolean useVideo)
	{
		Log.d(THIS_FILE, "CallExtStrInfo useVideo = " + useVideo + " callType = " + callType);
		this.callType = callType;
		this.caller = caller;
		this.callee = callee;
		this.useVideo = useVideo;
	}
	
	public CallExtStrInfo(int callType)
	{
		Log.d(THIS_FILE, "CallExtStrInfo useVideo = " + useVideo + " callType = " + callType);
		this.callType = callType;
//		this.caller = caller;
//		this.callee = callee;
//		this.useVideo = useVideo;
	}
	
	public CallExtStrInfo(String extStr)
	{
		Log.d(THIS_FILE, "CallExtStrInfo extStr = " + extStr);
		extStr = extStr.trim();
		if(extStr.startsWith(":"))
		{
			extStr = extStr.substring(1,extStr.length() -1);
		}
		Log.d(THIS_FILE, "CallExtStrInfo extStr = " + extStr);
		String[] strArr = extStr.split(";");
		valueMap = new HashMap<String,String>();
		for(int i = 0; i < strArr.length; i++)
		{
			String value = strArr[i];
			if(!value.contains("="))
			{
				this.callTypeStr = strArr[i].trim();
			}
			else
			{
				String[] valueArr = value.split("=");
				if(valueArr.length == 2)
				{
					if(valueArr[0] != null
							&& valueArr[1] != null)
					{
						valueMap.put(valueArr[0].trim(), valueArr[1].trim());
					}
				}
			}
		}
	}
	
	public int getCallType()
	{
		Log.d(THIS_FILE, "updateCallInfoFromStack callTypeStr = " + callTypeStr);
		return getCallTypeFromDesc(callTypeStr);
	}
	
	public boolean hasVideo()
	{
		String val = getValue(Key_UseVideo);
		return CheckData.parseBoolean(val);
	}
	
	public String getValue(String key)
	{
		if(key == null
				|| key.equals(""))
		{
			return DefauleValue;
		}
		
		String value = valueMap.get(key);
		if( value != null ) {
			return value;
		}
		
		return DefauleValue;
	}
	
	public String toString()
	{
		StringBuffer strBuf = new StringBuffer();
		String callTypeStr = getCallTypeDesc(callType,useVideo);
		strBuf.append(callTypeStr).append(";");
		if(callType == CallAttr.CallType_Group
				|| callType == CallAttr.CallType_Video_PushGrp
				|| callType == CallAttr.CallType_Video_TransGrp)
		{
			strBuf.append("Ctype=orig;");
			strBuf.append(Key_GId).append("=").append(callee).append(";");
		}
		else
		{
			strBuf.append("Ctype=orig;");
//			strBuf.append(Key_Caller).append("=").append(caller).append(";");
			strBuf.append(Key_Callee).append("=").append(callee).append(";");
		}
		if(callType != CallAttr.CallType_Group)
		{
			strBuf.append(Key_Direct).append("=").append(getDirectStr()).append(";");
		}
		//strBuf.append(Key_UseVideo).append("=").append(useVideo).append(";");
		return strBuf.toString();
	}
	
	public String to200OkString()
	{
		StringBuffer strBuf = new StringBuffer();
		if(callType != CallAttr.CallType_Group)
		{
			strBuf.append(Key_Direct).append("=").append(get200oKDirectStr()).append(";");
		}
		return strBuf.toString();
	}
	
	
	private String get200oKDirectStr()
	{
		if(callType == CallAttr.CallType_Single)
		{
			return "sendrecv";
		}
		else if(callType == CallAttr.CallType_Video_Push
				|| callType == CallAttr.CallType_Video_PushGrp
				|| callType == CallAttr.CallType_Video_Trans
				|| callType == CallAttr.CallType_Video_TransGrp)
		{
			return "recvonly";
		}
		else if(callType == CallAttr.CallType_Video_Pull)
		{
			return "sendonly";
		}
		return "";
	}
	private String getDirectStr()
	{
		if(callType == CallAttr.CallType_Single)
		{
			return "sendrecv";
		}
		else if(callType == CallAttr.CallType_Video_Push
				|| callType == CallAttr.CallType_Video_PushGrp
				|| callType == CallAttr.CallType_Video_Trans
				|| callType == CallAttr.CallType_Video_TransGrp)
		{
			return "sendonly";
		}
		else if(callType == CallAttr.CallType_Video_Pull)
		{
			return "recvonly";
		}
		return "";
	}
	
	public static int getCallTypeFromDesc(String desc)
	{
		if(CallType_Desc_Group.toLowerCase().equals(desc))
		{
			return CallAttr.CallType_Group;
		}
		else if(CallType_Desc_Single_Aud.toLowerCase().equals(desc))
		{
			return CallAttr.CallType_Single;
		}
		else if(CallType_Desc_Single_Vid.toLowerCase().equals(desc))
		{
			return CallAttr.CallType_Single;
		}
		else if(CallType_Desc_Single_Vid_Push.toLowerCase().equals(desc))
		{
			return CallAttr.CallType_Video_Push;
		}
		else if(CallType_Desc_Single_Vid_Pull.toLowerCase().equals(desc))
		{
			return CallAttr.CallType_Video_Pull;
		}
		else if(CallType_Desc_Single_Half.toLowerCase().equals(desc))
		{
			return CallAttr.CallType_Single_half;
		}
		
		else if(CallType_Desc_Single_Vid_Trans.toLowerCase().equals(desc))
		{
			return CallAttr.CallType_Video_Trans;
		}
		else if(CallType_Desc_Single_Vid_TransGrp.toLowerCase().equals(desc))
		{
			return CallAttr.CallType_Video_TransGrp;
		}
		else if(CallType_Desc_Single_Vid_PushGrp.toLowerCase().equals(desc))
		{
			return CallAttr.CallType_Video_PushGrp;
		}
		return CallAttr.CallType_Single;
	}

	public static String getCallTypeDesc(int callType,boolean useVideo)
	{
		
		if(callType == CallAttr.CallType_Group)
		{
			return CallType_Desc_Group;
		}
		else if(callType == CallAttr.CallType_Single)
		{
			if(useVideo)
			{
				return CallType_Desc_Single_Vid;
			}
			else
			{
				return CallType_Desc_Single_Aud;
			}
		}
		if(callType == CallAttr.CallType_Single_half)
		{
			return CallType_Desc_Single_Half;
		}
		if(callType == CallAttr.CallType_Video_Push)
		{
			return CallType_Desc_Single_Vid_Push;
		}
		else if(callType == CallAttr.CallType_Video_Pull)
		{
			return CallType_Desc_Single_Vid_Pull;
		}
		
		else if(callType == CallAttr.CallType_Video_Trans)
		{
			return CallType_Desc_Single_Vid_Trans;
		}
		else if(callType == CallAttr.CallType_Video_TransGrp)
		{
			return CallType_Desc_Single_Vid_TransGrp;
		}
		else if(callType == CallAttr.CallType_Video_PushGrp)
		{
			return CallType_Desc_Single_Vid_PushGrp;
		}
		else
		{
			return CallType_Desc_Single_Aud;
		}
	}
	
	
}
